package com.zxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxl.entity.Goods;
import com.zxl.entity.Stock;
import com.zxl.service.IGoodsService;
import com.zxl.service.IGoodsTypeService;
import com.zxl.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @packageName com.zxl.controller
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsTypeService goodsTypeService;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IStockService stockService;


    @RequestMapping("/list")
    public List<Goods> list(){
        List<Goods> list = goodsService.list();
        for (Goods goods : list) {
            goods.setGoodsType(goodsTypeService.getById(goods.getTypeid()));
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("gid",goods.getId());
            List list1 = stockService.list(wrapper);
            goods.setStocks(list1);
        }
        return list;
    }

    @PostMapping("/add")
    public Goods addGoods(@RequestBody Goods goods){
        return goodsService.saveGoodsAndStock(goods);
    }
}
